/**
 * ${moduleName}管理初始化
 */
var ${entity} = {
    id: "${entity}Table",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
${entity}.initColumn = function () {
    return [
        {width: '3%', field: 'selectItem', checkbox: true},
        <#list table.fields as field>
        <#if field.keyFlag>
        {width: '5%',title: '主键', field: '${field.propertyName}', visible: true, align: 'center', valign: 'middle'},
        <#elseif !field.keyFlag && field.propertyName != "sort" && field.propertyName != "status">
        {title: '${field.comment}', field: '${field.propertyName}', visible: true, align: 'center', valign: 'middle'},
        </#if>
        <#if field.propertyName == "status">
        {width: '7%', title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return value === 1 ? "有效" : "无效";
            }
        },
        </#if>
        <#if field.propertyName == "sort">
        {width: '10%', title: '排序', field: 'sort', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return '<input maxlength="9" onkeyup="${entity}.checkSortInput(' + row.id + ')" id="input_' + row.id + '" style="width: 30px;" value="' + value + '"/>' +
                '&nbsp;<button class="btn btn-primary btn-sm" onclick="${entity}.changeSort(' + row.id + ');">调序</button>';
            }
        },
        </#if>
        </#list>
        {width: '20%',title: '操作', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                let str = '';
                str = str + '&nbsp;<button class="btn btn-primary btn-sm" onclick="${entity}.open${entity}Detail(' + row.id + ');"><i class="fa fa-edit"></i>&nbsp;修改</button>';
                <#list table.fields as field>
                <#if field.propertyName == "status">
                if (row.status === 0) {
                    str = str + '&nbsp;<button type="button" class="btn btn-primary btn-sm"  onclick="${entity}.changeStatus(' + row.id + ',1);" id="3"><i class="fa fa-edit"></i>&nbsp;置为有效</button>';
                } else if (row.status === 1) {
                    str = str + '&nbsp;<button type="button" class="btn btn-warning btn-sm" onclick="${entity}.changeStatus(' + row.id + ',0);" id="3"><i class="fa fa-edit"></i>&nbsp;置为无效</button>';
                }
                </#if>
                </#list>
                return str;
            }
        }
    ];
};

/**
 * 检查是否选中
 */
${entity}.check = function () {
    const selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length === 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ${entity}.seItem = selected[0];
        return true;
    }
};

${entity}.checkSortInput = function (id) {
    const val = $("#input_" + id).val();
    if (val === "" || val === null) {
        return false;
    }
    return !isNaN(val);
}

<#list table.fields as field>
<#if field.propertyName == "sort">
/**
 * 修改排序
 */
${entity}.changeSort = function (id) {
    if (${entity}.checkSortInput(id)) {
        Feng.confirm("会自动清除缓存，确定修改排序吗？", function () {
        const ajax = new $ax(Feng.ctxPath + "/${package.Parent}/sort", function (data) {
            Feng.success("修改成功!");
            ${entity}.table.refresh();
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", id);
        ajax.set("sort", $("#input_" + id).val());
        ajax.start();
        });
    }
}
</#if>
<#if field.propertyName == "status">
/**
 * 修改状态
 */
${entity}.changeStatus = function (id, status) {
    const flag = function () {
        const ajax = new $ax(Feng.ctxPath + "/${package.Parent}/status", function (data) {
            Feng.success("修改成功!");
            ${entity}.table.refresh();
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", id);
        ajax.set("status", status);
        ajax.start();
    };
    Feng.confirm("会自动清除缓存，确定要修改状态吗?", flag);
}
</#if>
</#list>
/**
 * 删除${moduleName}
 */
${entity}.delete = function (id) {
    let ids = "";
    if (id !== 0) {
        ids = id;
    } else {
        const selected = $('#${entity}Table').bootstrapTable('getSelections');
        if (selected.length === 0) {
            Feng.info("请先选中表格中的某一记录！");
            return;
        }
        $.each(selected, function (index, o) {
            const id = o.id;
            if (Feng.isNotNullOrEmpty(id)) {
                ids += id + ","
            }
        });
        if (Feng.isNotNullOrEmpty(ids)) {
            ids = ids.substring(0, ids.length - 1);
        }
    }
    Feng.confirm("确定删除该记录吗？", function () {
        const ajax = new $ax(Feng.ctxPath + "/${package.Parent}/delete", function (data) {
            Feng.success("删除成功!");
            ${entity}.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("data", ids);
        ajax.start();
    })
};

/**
 * 点击添加${moduleName}
 */
${entity}.openAdd${entity} = function () {
    const index = layer.open({
        type: 2,
        title: '添加${moduleName}',
        area: ['80%', '50%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/${package.Parent}/${package.Parent}_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看${moduleName}详情
 */
${entity}.open${entity}Detail = function (id) {
    const index = layer.open({
        type: 2,
        title: '${moduleName}详情',
        area: ['80%', '50%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/${package.Parent}/${package.Parent}_update/' + id
    });
    this.layerIndex = index;
};

${entity}.formParams = function () {
    const queryData = {};
    queryData['id'] = $("#id").val();
    queryData['status'] = $("#status").val();
    return queryData;
}

/**
 * 查询${moduleName}列表
 */
${entity}.search = function () {
    const queryData = ${entity}.formParams();
    ${entity}.table.refresh({query: queryData});
};

$(function () {
    const defaultColumns = ${entity}.initColumn();
    const table = new BSTable(${entity}.id, "/${package.Parent}/list", defaultColumns);
    table.setPaginationType("server");
    table.setQueryParams(${entity}.formParams())
    ${entity}.table = table.init();
});
